package com.zzw.basic.io.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutionException;

public class AIOClient {

    static final int port = 30000;

    public static void main(String[] args) {
        ByteBuffer buff = ByteBuffer.allocate(1024);
        Charset utf = Charset.forName("utf-8");
        ByteBuffer sendMsg = ByteBuffer.allocate(1024);

        try {
            AsynchronousSocketChannel channel = AsynchronousSocketChannel.open();
            channel.connect(new InetSocketAddress("127.0.0.1",port)).get();
            buff.clear();
            channel.read(buff).get();
            buff.flip();
            String content = utf.decode(buff).toString();
            System.out.println("服务器信息:" + content);
            channel.write(ByteBuffer.wrap("你好".getBytes("UTF-8"))).get();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
